package com.zghhost.codedemo.sshclient;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

/**
 * @author guohua.zhang@zhaopin.com.cn
 * @Date 2019/4/1 11:21
 */
public class SSHTest {
    public static void main(String[] args)throws Exception{
        JSch jsch=new JSch();
        //set private key for auth
        jsch.addIdentity("yangtianxin_pc");

        Session session=jsch.getSession("bellring", "10.2.48.179", 22);
        session.setConfig("StrictHostKeyChecking", "no");

        //set auth info interactively
        //session.setUserInfo(new UserInfo(){.....});

        //session.setPassword("bellring");
        session.connect();

        com.jcraft.jsch.ChannelExec ec=(com.jcraft.jsch.ChannelExec)session.openChannel("exec");
        ec.setCommand("ifconfig");
        ec.setInputStream(null);
        ec.setErrStream(System.err);
        ec.setOutputStream(System.out);
        ec.connect();

        while(!ec.isClosed())
            Thread.sleep(500);

        ec.disconnect();
        session.disconnect();
    }
}
